package ViewPropertyPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import drawPropertyCommands.*;


/**
 * Create a JToolBar and populate it with buttons attached with DrawProperties commands
 * This provides a graphical way to modify drawing styles for the turtle trails
 * 
 * There is great potential for refactoring in the class with use of reflection. However,
 * we don't have enough time to make the changes.
 * @author KevinWang
 *
 */
public class ViewPropertyToolBar extends JToolBar{

    private static final long serialVersionUID = 1L;
    private ActionListener actionListener;
    
    //TODO use resource bundle instead of hard coding the buttons here
    private String[][] widthList= {{"stroke1", "stroke1.png","1f"},
            {"stroke2", "stroke2.png","2f"},
            {"stroke3", "stroke3.png","3f"}};
    
    private String[][] dashList={{"solid", "solid.png", "1f"},
            {"dashed", "dashed.png", "5.0f"}};

    private String[][] colorList={{"red", "red.png", "red"},
            {"blue", "blue.png", "blue"}};
    
    private String imagePath = "src/image/";


    public ViewPropertyToolBar(ActionListener al){
        super();
        actionListener = al;
        this.setOrientation(JToolBar.VERTICAL);
        this.setVisible(true);
        addButtons();

    }

    /**
     * populate the JToolBar with buttons
     */
    public void addButtons(){
        addWidthList();
        this.addSeparator();
        addDashList();
        this.addSeparator();
        addColorList();
        
    }


    /**
     * add buttons with modifiers that will change the dash property of the GraphicsProperties
     */
    protected void addDashList() {
        for(String[] argument: dashList){
            float[] dash = {Float.parseFloat(argument[2])};
            String name = argument[0];
            ImageIcon icon = new ImageIcon(imagePath+argument[1]);
            this.add(createButton(new DashModifier(dash),name, icon ));
        }
    }
    
    /**
     * add buttons with modifiers that will change the width property of the graphics properties
     */
    protected void addWidthList(){
        for(String[] argument: widthList){
            float width = Float.parseFloat(argument[2]);
            String name = argument[0];
            ImageIcon icon = new ImageIcon(imagePath+argument[1]);
            this.add(createButton(new WidthModifier(width),name, icon ));
        }
    }
    
    /**
     * add buttons with color modifiers that will change the color properties of the GraphicsProperties
     */
    protected void addColorList(){
        for(String[] argument: colorList){
            Color color= null;
            try {
                color = (Color)(Class.forName("java.awt.Color").getField(argument[2]).get(null));
            } catch (Exception e) {
                e.printStackTrace();
            }
            String name = argument[0];
            ImageIcon icon = new ImageIcon(imagePath+argument[1]);
            this.add(createButton(new ColorModifier(color),name, icon ));
        }
    }
    
    /**
     * create the appropriate button
     * @param modifier-DrawProperties commands to be performed
     * @param name- string representing name of the button
     * @param icon- icon of the button
     * @return
     */
    public JButton createButton(IDrawModifier modifier, String name, Icon icon){
        return new DrawModifierButtons(modifier, name, actionListener, icon);
    }


}
